package com.caption.core.executor;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池仓库
 */
public class WorkExecutorRepository {
    private static ConcurrentHashMap<String, ThreadPoolExecutor> workExecutorMap;

    public WorkExecutorRepository() {
        workExecutorMap = new ConcurrentHashMap<>();
    }

    public static class SingletonHandler {
        final static WorkExecutorRepository instance = new WorkExecutorRepository();
    }

    public static WorkExecutorRepository getInstance() {
        return SingletonHandler.instance;
    }

    public ThreadPoolExecutor bind(String executorName, ThreadPoolExecutor threadPoolExecutor) {
        return workExecutorMap.putIfAbsent(executorName, threadPoolExecutor);
    }

    public ThreadPoolExecutor unbind(String executorName) {
        return workExecutorMap.remove(executorName);
    }

    public ThreadPoolExecutor lookup(String executorName) {
        return workExecutorMap.get(executorName);
    }
}
